﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Data;
using System.Windows.Controls.Primitives;

namespace IconEditorXBAP.IconElements
{
    public class MoRectangle : BasicShape
    {
        private ModelicaElements.Rectangle src;   // Modelica Rectangle
        Rectangle obj = new Rectangle();

        private MoRectangle()
            : base()
        {
            Grid.SetColumn(obj, 1);
            Grid.SetRow(obj, 1);
            obj.Stretch = Stretch.Fill;
            obj.MouseLeftButtonDown += new MouseButtonEventHandler(BasicShape_MouseLeftButtonDown);
            obj.MouseMove += new MouseEventHandler(BasicShape_MouseMove);
            obj.MouseLeftButtonUp += new MouseButtonEventHandler(BasicShape_MouseLeftButtonUp);
            obj.Margin = new Thickness(-4, -4, -4, -4);
            Panel.SetZIndex(obj, 1);

            LayoutRoot.Children.Add(obj);
        }

        public MoRectangle(ModelicaElements.Rectangle _rectangle)
            : this()
        {
            src = _rectangle;
            LayoutRoot.DataContext = src;

            #region Bind Rectangle Dependency properties to Modelica Rectangle object

            Binding bind;

            bind = new Binding();   // visibility
            bind.Path = new PropertyPath("visible");
            bind.Converter = new VisbilityConverter();
            obj.SetBinding(Rectangle.VisibilityProperty, bind);

            bind = new Binding();   // fill color
            bind.Path = new PropertyPath("fillColor");
            bind.Converter = new ColorToFillConverter();
            obj.SetBinding(Rectangle.FillProperty, bind);

            bind = new Binding();   // line color
            bind.Path = new PropertyPath("lineColor");
            bind.Converter = new ColorToFillConverter();
            obj.SetBinding(Rectangle.StrokeProperty, bind);

            bind = new Binding();   // line thickness
            bind.Path = new PropertyPath("lineThickness");
            obj.SetBinding(Rectangle.StrokeThicknessProperty, bind);

            bind = new Binding();   // radius
            bind.Path = new PropertyPath("radius");
            obj.SetBinding(Rectangle.RadiusXProperty, bind);
            obj.SetBinding(Rectangle.RadiusYProperty, bind);

            bind = new Binding();   // width
            bind.Path = new PropertyPath("extent");
            bind.Converter = new ExtentToWidthConverter();
            obj.SetBinding(Rectangle.WidthProperty, bind);

            bind = new Binding();   // height
            bind.Path = new PropertyPath("extent");
            bind.Converter = new ExtentToHeightConverter();
            obj.SetBinding(Rectangle.HeightProperty, bind);

            bind = new Binding();   // left
            bind.Source = src;
            bind.Path = new PropertyPath("extent");
            bind.Converter = new ExtentToCanvasLeftConverter();
            this.SetBinding(Canvas.LeftProperty, bind);

            bind = new Binding();   // top
            bind.Source = src;
            bind.Path = new PropertyPath("extent");
            bind.Converter = new ExtentToCanvasTopConverter();
            this.SetBinding(Canvas.TopProperty, bind);

            #endregion
        }
       
        internal override string GetAnnotation()
        {
            //Rectangle(visible=true, lineColor={0,0,255}, fillColor={0,0,255}, fillPattern=FillPattern.Solid, extent={{-99.71,-84.51},{100.2,-44.46}},
            String s = "Rectangle(visible=true, lineColor={0,0,255}, fillColor={0,0,255}, fillPattern=FillPattern.Solid, extent={{";

            s += Math.Round(src.extent[0, 0], 2).ToString();
            s += ",";
            s += Math.Round(src.extent[0, 1], 2).ToString();
            s += "},{";

            s += Math.Round(src.extent[1, 0], 2).ToString();
            s += ",";
            s += Math.Round(src.extent[1, 1], 2).ToString();
            s += "}}),";

            return s;
        }

        protected override void BasicShape_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseCaptured)
            {
                // Calculate the current position of the object.
                double deltaH = e.GetPosition(null).X - mousePosition.X;
                double deltaV = e.GetPosition(null).Y - mousePosition.Y;

                src.extent[0, 0] += deltaH / 2;
                src.extent[1, 0] += deltaH / 2;
                src.extent[0, 1] += deltaV / 2;
                src.extent[1, 1] += deltaV / 2;

                // Update position global variables.
                mousePosition = e.GetPosition(null);
                src.NotifyPropertyChanged("extent");
            }
        }

        protected override void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            //double deltaVertical, deltaHorizontal;

            FrameworkElement item = sender as FrameworkElement;
            switch (item.VerticalAlignment)
            {
                case System.Windows.VerticalAlignment.Bottom:
                    //deltaVertical = Math.Min(e.VerticalChange, this.ActualHeight - this.MinHeight);
                    src.extent[1, 1] += e.VerticalChange;
                    break;
                case System.Windows.VerticalAlignment.Top:
                    //deltaVertical = Math.Min(e.VerticalChange, this.ActualHeight - this.MinHeight);
                    src.extent[0, 1] += e.VerticalChange;
                    break;
                default:
                    break;
            }

            switch (item.HorizontalAlignment)
            {
                case System.Windows.HorizontalAlignment.Left:
                    //deltaHorizontal = Math.Min(e.HorizontalChange, this.ActualWidth - this.MinWidth);
                    src.extent[0, 0] += e.HorizontalChange;
                    break;
                case System.Windows.HorizontalAlignment.Right:
                    //deltaHorizontal = Math.Min(e.HorizontalChange, this.ActualWidth - this.MinWidth);
                    src.extent[1, 0] += e.HorizontalChange;
                    break;
                default:
                    break;
            }

            e.Handled = true;
            src.NotifyPropertyChanged("extent");
        }
    }
}
